home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / dev / cross / 6502d.lha / 6502d.c next >
Encoding:
C/C++ Source or Header  |  2002-01-26  |  3.9 KB  |  103 lines

  1. /*****************************************************************************
  2. * 6502D Version 0.1                                                          *
  3. * Bart Trzynadlowski, 1999                                                   *
  4. *                                                                            *
  5. * Feel free to do whatever you wish with this source code provided that you  *
  6. * understand it is provided "as is" and that the author will not be held     *
  7. * responsible for anything that happens because of this software.            *
  8. *                                                                            *
  9. * 6502d.c: Main code for the 6502 disassembler.                              *
  10. *****************************************************************************/
  11.  
  12. /* the source code is hideous, live with it =) */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "6502d.h"
  17.  
  18. #define USAGE_TEXT "Usage: 6502d -f infile [-s start] [-h] [-v]\nTry \"6502d -h\" for help\n"
  19. #define VERSION_TEXT "6502d version 0.1\n"
  20. #define HELP_TEXT "6502d -- Free 6502 Disassembler by Bart Trzynadlowski\n" \
  21.                   "\n" \
  22.                   "Usage: 6502d -f infile [-s start] [-h] [-v]\n" \
  23.                   "All arguments in brackets, [], are optional. Case sensitivity applies.\n" \
  24.                   "\n" \
  25.                   "Argument Descriptions:\n" \
  26.                   "\t-f infile:\tSpecifies the input binary file.\n" \
  27.                   "\t-s start:\tStarting point of disassembly (in base 16).\n" \
  28.                   "\t-h:\tPrints this help text. All other arguments ignored, except -v.\n" \
  29.                   "\t-v:\tPrints version. All other arguments ignored.\n" \
  30.                   "\n" \
  31.                   "Feel free to do whatever you wish with this software as long as you understand\n" \
  32.                   "it is provided \"as is\" and the author will not be held liable for anything that\n" \
  33.                   "happens in relation with it.\n"
  34. #define ERROR_ARGUMENT_MSG "6502d: error, missing or invalid argument(s)\n"
  35.  
  36. int main(int argc, char *argv[])
  37. {       
  38.         int i;
  39.         int *p=&i;      /* pointer to i */
  40.         int if_index;   /* index into argv[] pointing to infile name */
  41.         FILE *infile;
  42.         unsigned long start=0;
  43.         long filesize;
  44.         unsigned char opcode;
  45.  
  46.         /* read arguments. if argc==1 then only the path was recorded */
  47.         if (argc==1)
  48.         {
  49.                 printf(USAGE_TEXT);
  50.                 return 0;
  51.         }
  52.  
  53.         /* check for -r */
  54.         if (singlearg(argc, argv, "-v"))
  55.         {
  56.                 printf(VERSION_TEXT);
  57.                 return 0;
  58.         }
  59.         /* check for -h */
  60.         if (singlearg(argc, argv, "-h"))
  61.         {
  62.                 printf(HELP_TEXT);
  63.                 return 0;
  64.         }
  65.         /* get -f argument */
  66.         if_index=findarg(argc, argv, "-f");
  67.         if (if_index==0)        /* no -f on command line? */
  68.         {
  69.                 printf(ERROR_ARGUMENT_MSG);
  70.                 return 1;
  71.         }
  72.         /* get -s argument if it exists */
  73.         if (findarg(argc, argv, "-s")!=0)
  74.                 start=strtoul(argv[findarg(argc, argv, "-s")], NULL, 16);
  75.         /* open the files */
  76.         if ((infile=fopen(argv[if_index], "rb"))==NULL)
  77.         {
  78.                 printf("6502d: error, could not open \"%s\"\n", argv[if_index]);
  79.                 return 1;
  80.         }
  81.  
  82.         /* get length of file, seek 0 bytes from end */
  83.         fseek(infile, 0, SEEK_END);
  84.         filesize=ftell(infile);
  85.         fseek(infile, start, SEEK_SET);
  86.  
  87.         /* main loop */
  88.         for (i=start;i!=filesize;i++)
  89.         {
  90.                 
  91.                 opcode=fgetc(infile);
  92.                 /* if last byte then just do a db $XX */
  93.                 if (i>=filesize-1-2)
  94.                         printf("%08X:\t%02X\t.DB $%02X\n", i, opcode, opcode);
  95.                 else
  96.                         disasm(opcode, p, infile);
  97.         }
  98.  
  99.         return 0;
  100. }
  101.  
  102.  
  103.